package com.ywjh.inter.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.service.RtcService;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONObject;

public class Token extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6659242391181396802L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");
		int r = 0;
		String e = "";

		JSONObject postData = StrUtil.getPostJSON(req);
		String act = postData.getString("act");
		JSONObject data = postData.getJSONObject("data");
		System.out.println("token="+postData.toString());
		JSONObject json = new JSONObject();
		switch (act) {
		case "get":
			String token=RtcService.getInstance().token(data.getString("channel_name"), data.getString("user_account"));
			r=1;
			json.put("d", token);
			break;
		}
		json.put("r", r);
		json.put("e", e);
		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();
	}
	
}
